#include "func.h"
#include "netfd_list.h"

list_t *create_list() {
    return (list_t *)calloc(1, sizeof(list_t));
}

void destroy_list(list_t *list) {
    // 1.释放所有节点
    node_t *cur = list->head;
    while (cur) {
        node_t *temp = cur;
        cur = cur->next;
        free(temp);
    }

    // 2.释放链表
    free(list);
}

void add_before_head(list_t *list, int val) {
    node_t *node = (node_t *)malloc(sizeof(node_t));
    node->val = val;

    // 1.首先判断链表是否为空
    if (!list->tail) {
        list->head = node;
        list->tail = node;
        node->next = NULL;
    } else {
        // 2.链表不为空的情况
        node->next = list->head;
        list->head = node;
    }

    list->size++;
}

void add_behind_tail(list_t *list, int val) {
    node_t *node = (node_t *)malloc(sizeof(node_t));
    node->val = val;

    // 1.首先判断链表是否为空
    if (!list->head) {
        list->head = node;
        list->tail = node;
        node->next = NULL;
    } else {
        // 2.链表不为空的情况
        node->next = NULL;
        list->tail->next = node;
        list->tail = node;
    }

    list->size++;
}

void add_node(list_t *list, int idx, int val) {
    if (idx < 0 || idx > list->size) {
        printf("Illegal argument, idx: %d, size: %d\n", idx, list->size);
        exit(1);
    }

    if (idx == 0) {
        add_before_head(list, val);
        return;
    }

    if (idx == list->size) {
        add_behind_tail(list, val);
        return;
    }

    node_t *cur = list->head;
    for (int i = 0; i < idx - 1; ++i) {
        cur = cur->next;
    }

    node_t *node = (node_t *)malloc(sizeof(node_t));
    node->val = val;

    node->next = cur->next;
    cur->next = node;

    list->size++;
}

/**
 * 在list中删除第一个与 val 相等的结点
 * @param list
 * @param val
 */
void delete_node(list_t *list, int val) {
    node_t *cur = list->head;
    node_t *pre = NULL;
    while (cur->next) {
        if (cur->val == val) {
            // 1.如果删除的是头节点
            if (!pre) {
                list->head = cur->next;
                free(cur);
                // 如果只有这一个节点
                if (!list->head) {
                    list->tail = NULL;
                }
                list->size--;
            } else {
                pre->next = cur->next;
                // 如果删除的是最后一个节点
                if (!cur->next) {
                    list->tail = pre;
                }

                free(cur);
                list->size--;
            }
            return;
        }

        pre = cur;
        cur = cur->next;
    }
}

node_t *find_by_index(list_t *list, int idx) {
    if (idx < 0 || idx >= list->size) {
        printf("Illegal argument, idx: %d, size: %d\n", idx, list->size);
        exit(1);
    }

    node_t *cur = list->head;
    while (idx--) {
        cur = cur->next;
    }

    return cur;
}

/**
 * 在list中查找第一个与val值相等的结点
 * @param list
 * @param val
 * @return
 */
node_t *search_for_value(list_t *list, int val) {
    node_t *cur = list->head;

    while (cur) {
        if (cur->val == val) {
            return cur;
        }

        cur = cur->next;
    }

    return NULL;
}
